package com.lifesting.ganttviewer;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TreeColumn;

import com.lifesting.ganttviewer.model.GPredecessor;
import com.lifesting.ganttviewer.model.GProject;
import com.lifesting.ganttviewer.model.GTask;
import com.lifesting.ganttviewer.model.GWorkingItem;

public class TaskViewer extends TreeViewer {
	private GProject project;
	public static final Object[] EMPTY = new Object[0];
	private SimpleDateFormat formatter;
	public TaskViewer(Composite parent) {
		super(parent,SWT.FULL_SELECTION);
		formatter = new SimpleDateFormat("yyyy-MM-dd E", Locale.CHINA);
		getTree().setLinesVisible(true);
		getTree().setHeaderVisible(true);
		
		addColumn();
	}
	private void addColumn() {
		TreeColumn tc_name = new TreeColumn(getTree(),SWT.LEFT);
		TreeColumn tc_duration = new TreeColumn(getTree(),SWT.LEFT);
		TreeColumn tc_start = new TreeColumn(getTree(),SWT.LEFT);
		TreeColumn tc_finish = new TreeColumn(getTree(),SWT.LEFT);
		TreeColumn tc_predecessor = new TreeColumn(getTree(),SWT.LEFT);
		tc_name.setText("Task Name");
		tc_name.setWidth(200);
		tc_duration.setText("Duration");
		tc_duration.setWidth(55);
		tc_start.setText("Start");
		tc_start.setWidth(120);
		tc_finish.setText("Finish");
		tc_finish.setWidth(120);
		tc_predecessor.setText("Predecessors");
		tc_predecessor.setWidth(150);
	}
	public void setProject(GProject project){
		this.project = project;
		setContentProvider(new ITreeContentProvider(){

			@Override
			public Object[] getChildren(Object parentElement) {
				List<GWorkingItem> sub = ((GWorkingItem)parentElement).getContains();
				if (sub == null) return EMPTY;
				return sub.toArray();
			}

			@Override
			public Object getParent(Object element) {
				return ((GWorkingItem)element).getGroup();
			}

			@Override
			public boolean hasChildren(Object element) {
				GWorkingItem item = (GWorkingItem) element;
				return (item.getContains() != null && item.getContains().size() > 0);
			}

			@Override
			public Object[] getElements(Object inputElement) {
				return getChildren(inputElement);
			}

			@Override
			public void dispose() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void inputChanged(Viewer viewer, Object oldInput,
					Object newInput) {
				// TODO Auto-generated method stub
				
			}});
		setLabelProvider(new ITableLabelProvider(){
			
			@Override
			public Image getColumnImage(Object element, int columnIndex) {
				return null;
			}

			@Override
			public String getColumnText(Object element, int columnIndex) {
				GWorkingItem item = (GWorkingItem) element;
				String result = null;
				switch (columnIndex){
				case 0:
					result = item.getId()+":"+item.getName();
					break;
				case 1:
					result = item.getDuration().getLabel();
					break;
				case 2:
					result = formatter.format(item.getStart());
					break;
				case 3:
					result = formatter.format(item.getFinish());
					break;
				case 4:
					result = format(((GTask)item).getPredecessors());
					break;
				default:
					result = "??";
					break;					
				}
				return result;
			}

			private String format(List<GPredecessor> predecessors) {
				if (predecessors == null) return "";
				StringBuffer sb = new StringBuffer();
				for (GPredecessor pd : predecessors){
					sb.append(pd.getTask().getId());
					sb.append('/');
				}
				return sb.toString();
			}

			@Override
			public void addListener(ILabelProviderListener listener) {
				
			}

			@Override
			public void dispose() {
				
			}

			@Override
			public boolean isLabelProperty(Object element, String property) {
				return true;
			}

			@Override
			public void removeListener(ILabelProviderListener listener) {
				
			}});
		setInput(project);
	}
}
